﻿#include "PopWndLayer.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GnsOften.h"
#include "SystemMgr.h"

USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()

PopWndLayer * PopWndLayer::popWndLayerSingleton = nullptr;
PopWndLayer::PopWndLayer()
{
    m_bIsClickWndOutSideClose=false;
    TouchEventSpt=0;
    pCustombutton=0;
    pCloseButton=0;
    pCombutton=0;
    m_bIsRunAction=true;
    m_bIsRunEffect=false;
    //_emitter=nullptr;
}

PopWndLayer::~PopWndLayer()
{
   // CC_SAFE_RELEASE(_emitter);
}

PopWndLayer* PopWndLayer::createScene(std::string pImageName,std::string pBtnName,bool bShow)
{
    auto pScene = Director::getInstance()->getRunningScene();
  if (!pScene)
      return 0;
   /*
    auto flag = false;
   
    if (!popWndLayerSingleton)
    {
        popWndLayerSingleton =  PopWndLayer::create();
        popWndLayerSingleton->retain();
        pScene->addChild(popWndLayerSingleton,9999);
     flag = true ;
   }
    popWndLayerSingleton->Setbackground(pImageName ,bShow);
    popWndLayerSingleton->SetColserBtn(pBtnName);
    Layer *   tempLayer;
     if (!flag){
        tempLayer = Layer::create();
        pScene->addChild(tempLayer,12);
     tempLayer->autorelease();
     }

    //    */
 
    auto pPopWndLayer = PopWndLayer::create();
    pPopWndLayer->Setbackground(pImageName ,bShow);
	if (pBtnName != "")
	{
		pPopWndLayer->SetColserBtn(pBtnName);
	}
    pScene->addChild(pPopWndLayer,9999);
//  */
    return pPopWndLayer;  //popWndLayerSingleton
}

bool PopWndLayer::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    initData();
    return true;
}

#pragma mark - initData
void PopWndLayer::initData()
{
    
    LayerColor* pLayer=LayerColor::create(Color4B(0,0,0,110));
    this->addChild(pLayer,0,11);
    // add your codes here...
    auto callback = [](Touch * ,Event *)
    {
        return true;
    };
    listener = EventListenerTouchOneByOne::create();
    listener->onTouchBegan = callback;
    listener->onTouchEnded =CC_CALLBACK_2(PopWndLayer::TouchEvents,this);
    listener->setSwallowTouches(true);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
    
    TouchEventSpt = Sprite::create();
    TouchEventSpt->setPosition(Vec2(0, 0));
    this->addChild(TouchEventSpt);
    
}

void PopWndLayer::TouchEvents(cocos2d::Touch *pTouch, cocos2d::Event *pEvents)
{
    Point pos = pTouch->getLocation();
    if (m_bIsClickWndOutSideClose) {
        if (PopwndBg) {
            TouchEventSpt->setPosition(pos);
            
            if (!TouchEventSpt->getBoundingBox().intersectsRect(PopwndBg->getBoundingBox()))
            {
                this->Show(false);
            }
        }
    }
}

Node* PopWndLayer::Loadcsb(std::string csbName)
{
    auto pPopWnd = CSLoader::createNode(csbName);
    pPopWnd->setContentSize(winsize);
    ui::Helper::doLayout(pPopWnd);
    pPopWnd->setPosition(Vec2(0,-PopwndBg->getContentSize().height/2));
    PopwndBg->addChild(pPopWnd,11);

	return pPopWnd;
}

void PopWndLayer::Setbackground(std::string pImagName ,bool bShow)
{
    PopwndBg = Sprite::create(pImagName);
    PopwndBg->setPosition(Vec2(winsize.width/2, winsize.height+PopwndBg->getContentSize().height));
    this->addChild(PopwndBg,1,10);
 
    if (bShow==true) {
        PopWndAction();
    }
}
void PopWndLayer::PopWndAction()
{
    auto _Move = MoveTo::create(1.0f, Vec2(winsize.width/2, winsize.height/2));
    auto rotateto = RotateTo::create(0.01f, -25);
    auto _rotateto =RotateTo::create(0.5f, 0);
    auto  easeBounceOut = EaseBounceOut ::create(_Move);
    auto pAction =Sequence::create(rotateto,easeBounceOut,_rotateto,NULL);
    PopwndBg->runAction(pAction);
}
/*
void PopWndLayer::PopWndEffectseffects(std::string EffectName ,bool bShow)
{
    if (bShow==true) {
        _emitter=ParticleSystemQuad::create(EffectName.c_str());
        _emitter->setPosition(Vec2(0, PopwndBg->getContentSize().height));
        PopwndBg->addChild(_emitter, 10);
        Size bgSize = PopwndBg->getContentSize();
        auto Moveto1 = MoveTo::create(0.5, Vec2(bgSize.width, bgSize.height));
        auto Moveto2 = MoveTo::create(0.5, Vec2(bgSize.width, 0));
        auto Moveto3 = MoveTo::create(0.5, Vec2(0, 0));
        auto Moveto4 = MoveTo::create(0.5, Vec2(0,bgSize.height));
        auto ActionNum = Sequence::create(Moveto1,Moveto2,Moveto3,Moveto4, NULL);
        _emitter->runAction(RepeatForever::create(ActionNum));
        _emitter->retain();
    }    
}
*/
void PopWndLayer::SetColserBtn(std::string pBtnName)
{
    pCloseButton=Button::create(pBtnName);
    pCloseButton->setPosition(Vec2(PopwndBg->getContentSize().width-pCloseButton->getContentSize().width/2, PopwndBg->getContentSize().height-pCloseButton->getContentSize().height/2));
    pCloseButton->setTag(PopWndCloser_TAG);
    pCloseButton->addTouchEventListener(CC_CALLBACK_2(PopWndLayer::BtnCallBack,this ));
    PopwndBg->addChild(pCloseButton,1);
}

void PopWndLayer::SetTitleWithUTF(std::string text,int fontsize)
{
    ltfTitle = Label::createWithSystemFont(CCGnsOften::getUtf8_DirectAssignmentInProgram(text), "", fontsize);
    ltfTitle->setPosition(Vec2(PopwndBg->getContentSize().width/2, PopwndBg->getContentSize().height*0.9));
    PopwndBg->addChild(ltfTitle,1);
}

void PopWndLayer::SetTitleWithGB(std::string text, int fontsize)
{
	ltfTitle = Label::createWithSystemFont(SystemMgr::getInstance()->GbToUTF8(text.c_str()), "", fontsize);
	ltfTitle->setPosition(Vec2(PopwndBg->getContentSize().width / 2, PopwndBg->getContentSize().height*0.9));
	PopwndBg->addChild(ltfTitle, 1);
}


void PopWndLayer::SetContentTextWithGB(std::string text,const Color3B&color,int fontsize,bool Shadow)
{
    ContentText=Text::create(SystemMgr::getInstance()->GbToUTF8(text.c_str()), "fonts/gbw3.ttc", fontsize);
    ContentText->setPosition(Vec2(PopwndBg->getContentSize().width/2, PopwndBg->getContentSize().height/2));
	ContentText->setTextAreaSize(Size(PopwndBg->getContentSize().width*0.75,PopwndBg->getContentSize().height/2));
    ContentText->setColor(color);
    ContentText->setTextHorizontalAlignment(TextHAlignment::CENTER);
	ContentText->setTextVerticalAlignment(TextVAlignment::CENTER);
    if (Shadow==true) {
        ContentText->enableShadow();
    }
    PopwndBg->addChild(ContentText,2);
    
}

void PopWndLayer::SetContentTextWithUTF(std::string text, const Color3B&color, int fontsize, bool Shadow)
{
	ContentText = Text::create(CCGnsOften::getUtf8_DirectAssignmentInProgram(text), "fonts/gbw3.ttc", fontsize);
	ContentText->setPosition(Vec2(PopwndBg->getContentSize().width / 2, PopwndBg->getContentSize().height / 2));
	ContentText->setTextAreaSize(Size(PopwndBg->getContentSize().width*0.75, PopwndBg->getContentSize().height / 2));
	ContentText->setColor(color);
	ContentText->setTextHorizontalAlignment(TextHAlignment::CENTER);
	ContentText->setTextVerticalAlignment(TextVAlignment::CENTER);
	if (Shadow == true) {
		ContentText->enableShadow();
	}
	PopwndBg->addChild(ContentText, 2);

}
Button * PopWndLayer::SetCompleteButton(std::string normalImage,std::string selectedImage,std::string disableImage)
{
    pCombutton = Button::create(normalImage,selectedImage,disableImage);
    pCombutton->addTouchEventListener(CC_CALLBACK_2(PopWndLayer::BtnCallBack, this));
    pCombutton->setPosition(Vec2(PopwndBg->getContentSize().width*0.3, PopwndBg->getContentSize().height*0.2));
    PopwndBg->addChild(pCombutton,1);
    return pCombutton;
}

Button * PopWndLayer::AddButton(std::string normalImage,std::string selectedImage,std::string disableImage)
{
    pCustombutton = Button::create(normalImage,selectedImage,disableImage);
    pCustombutton->setPosition(Vec2(PopwndBg->getContentSize().width*0.7, PopwndBg->getContentSize().height*0.3));
    PopwndBg->addChild(pCustombutton,1);
    return pCustombutton;
}
void PopWndLayer::AddCallBack(EventListenerTouchOneByOne::ccTouchCallback callback)
{
	if (nullptr != listener)
	{
		listener->onTouchEnded = callback;
	}
}
void PopWndLayer::Show(bool bShow)
{
    auto EndMove = MoveTo::create(0.5, Vec2(winsize.width/2, winsize.height*1.5));
    auto ActionCallBack =CallFunc::create([&](){
        _eventDispatcher->removeEventListener(listener);
        //CC_SAFE_RELEASE(_emitter);
        this->removeAllChildrenWithCleanup(!bShow);
    }
    );
    
    auto ActionSeq = Sequence::create(EndMove,ActionCallBack, NULL);
    PopwndBg->runAction(ActionSeq);
    
}

void PopWndLayer::setOutsideView(Node* bg)
{
    if (bg)
    {
        //bg->setPosition(Vec2(winsize.width/2,winsize.height*1.2));
        auto moveto = MoveTo::create(0.5f, Vec2(winsize.width/2,winsize.height*1.2));
        bg->runAction(moveto);
    }
}

void PopWndLayer::setClickWndOutsideClose(bool bClose)
{
    m_bIsClickWndOutSideClose=bClose;
}

void PopWndLayer::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case PopWndCloser_TAG:
            {
                this->Show(false);
                break;
            }
            case PopWndComplete_TAG:
            {
                break;
            }
            default:
                break;
        }
    }
}